package com.rules.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rules.admin.dao.RecentlyLogDao;
import com.rules.admin.dao.RecentlyPackageDao;
import com.rules.admin.entity.RecentlyLog;
import com.rules.admin.entity.RecentlyPackage;
import com.rules.admin.service.RecentlyLogService;
import com.rules.admin.vo.RuleUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.rules.admin.login.UserHolder;


import java.util.List;

@Service
public class RecentlyLogServiceImpl extends ServiceImpl<RecentlyLogDao, RecentlyLog> implements RecentlyLogService {
   @Autowired
   private UserHolder<RuleUser> userHolder;
   @Autowired
   private RecentlyPackageDao recentlyPackageDao;

   public boolean saveOrUpdate(RecentlyLog recentlyLog) {
      LambdaQueryWrapper<RecentlyLog> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(RecentlyLog::getTargetId,recentlyLog.getTargetId());
      queryWrapper.eq(RecentlyLog::getOwnerId,this.userHolder.getUser().getId());
      RecentlyLog logInDb = (RecentlyLog)this.getOne(queryWrapper);
      if (logInDb != null) {
         logInDb.setCount(logInDb.getCount() + 1);
         return this.updateById(logInDb);
      } else {
         recentlyLog.setOwnerId(this.userHolder.getUser().getId());
         recentlyLog.setCount(1);
         return this.save(recentlyLog);
      }

   }

   public List<RecentlyPackage> getRecentlyPackage() {
      String ownerId = userHolder.getUser().getUserId();
      return this.recentlyPackageDao.selectRecentlyPackages(ownerId);
   }
}
